import React, { useState, useEffect } from "react";
import marked from "marked";
import { Row, Col, Input, Select, Button, DatePicker, message } from "antd";
import "../common/AddArticle.css";
import axios from 'axios';
import servicePath from '../config/apiUrl'

const { Option } = Select;
const { TextArea } = Input;

const AddArticle = (props) => {
  const [articleId, setArticleId] = useState(0); // 文章的ID，如果是0说明是新增加，如果不是0，说明是修改
  const [articleTitle, setArticleTitle] = useState(""); //文章标题
  const [articleContent, setArticleContent] = useState(""); //markdown的编辑内容
  const [markdownContent, setMarkdownContent] = useState("预览内容"); //html内容
  const [introducemd, setIntroducemd] = useState(); //简介的markdown内容
  const [introducehtml, setIntroducehtml] = useState("等待编辑"); //简介的html内容
  const [showDate, setShowDate] = useState(); //发布日期
  const [updateDate, setUpdateDate] = useState(); //修改日志的日期
  const [typeInfo, setTypeInfo] = useState([]); // 文章类别信息
  const [selectedType, setSelectType] = useState(1); //选择的文章类别

  marked.setOptions({
    renderer: marked.Renderer(),
    gfm: true,
    pedantic: false,
    sanitize: false,
    tables: true,
    breaks: false,
    smartLists: true,
    smartypants: false,
  });

  const changeContent = (e) => {
    setArticleContent(e.target.value);
    let html = marked(e.target.value);
    setMarkdownContent(html);
  };

  const changeIntroduce = (e) => {
    setIntroducemd(e.target.value);
    let html = marked(e.target.value);
    setIntroducehtml(html);
  };

  const selectTypeHandler = (e) => {
    setSelectType(e)
  }

  // 编写文章保存方法
  const saveArticle = () => {
    if (!selectedType) {
      return message.error('必须选择文章类别')
    } else if (!articleTitle) {
      return message.error('文章名称不能为空')
    } else if (!articleContent) {
      return message.error('文章内容不能为空')
    } else if (!introducemd) {
      return message.error('简介不能为空')
    } else if (!showDate) {
      return message.error('发布日期不能为空')
    }
    message.success('检验通过')

    // 参数
    let params = {
      type_id: selectedType,
      title: articleTitle,
      article_content: articleContent,
      introduce: introducemd,
    }

    // 把字符串转换成时间戳
    let datatext = showDate.replace('-', '/');
    params.addTime = (new Date(datatext).getTime()) / 1000

    if (articleId == 0) {
      console.log('articleId=:'+articleId)
      params.view_count = Math.ceil(Math.random()*100) + 1000
      axios({
        method: 'post',
        url: servicePath.addArticle,
        data: params,
        withCredentials: true, // 后端允许跨域（cors), 前端也可以允许 请求带cookie
      }).then((res) => {
        setArticleId(res.data.insertId)
        if (res.data.insertSuccess) {
          message.success('文章保存成功')
        } else {
          message.error('文章保存失败')
        }
      })
    } else {
      params.id = articleId;
      axios({
        method: 'post',
        url: servicePath.updateArticle,
        header: { 'Access-Control-Allow-Origin': '*' },
        data: params,
        withCredentials: true, // 后端允许跨域（cors), 前端也可以允许 请求带cookie
      }).then((res) => {
        if (res.data.updateSuccess) {
          message.success('文章保存成功')
        } else {
          message.error('保存失败')
        }
      })
    }

  }

  // 从中台得到文章类别信息
  const getTypeInfo = () => {
    axios({
      method: 'get',
      url: servicePath.getTypeInfo,
      header: { 'Access-Control-Allow-Origin' : '*'},
      withCredentials: true, // 后端允许跨域（cors), 前端也可以允许 请求带cookie
    }).then(
      res => {
        if (res.data.data == '没有登录') {
          localStorage.removeItem('openId')
          props.history.push('/')
        } else {
          setTypeInfo(res.data.data)
        }
      }
    )
  }


  // 中台接口得到数据
  const getArticleById = (id) => {
    axios(servicePath.getArticleById, {
      withCredentials: true,
      header: { 'Access-Control-Allow-Origin': '*'}
    }).then((res) => {
      setArticleTitle(res.data.data[0].title)
      setArticleContent(res.data.data[0].article_content)
      let html = marked(res.data.data[0].article_content)
      setMarkdownContent(html)
      setIntroducemd(res.data.data[0].introduce)
      let tmpInt = marked(res.data.data[0].introduce)
      setIntroducehtml(tmpInt)
      setShowDate(res.data.data[0].addTime)
      setSelectType(res.data.data[0].typeId)
    })
  } 

  // 函数组件生命周期
  useEffect(() => {
    getTypeInfo()

    // 获取文章id
    let tmpId = props.match.params.id
    if (tmpId) {
      setArticleId(tmpId)
      getArticleById(tmpId)
    }
  }, [])

  return (
    <div>
      <Row gutter={5}>
        <Col span={18}>
          <Row gutter={10}>
            <Col span={20}>
              <Input placeholder="博客标题" size="large" value={articleTitle} onChange={e => setArticleTitle(e.target.value)}/>
            </Col>
            <Col span={4}>
              &nbsp;
              <Select defaultValue={selectedType} size="large" onChange={selectTypeHandler}>
                {
                  typeInfo.map((item, index) => {
                    return (<Option key={index} value={item.Id}>{item.typeName}</Option>)
                  })
                }
              </Select>
            </Col>
          </Row>
          <br />
          <Row gutter={10}>
            <Col span={12}>
              <TextArea
                value={articleContent}
                className="markdown-content"
                rows={35}
                onChange={changeContent}
                onPressEnter={changeContent}
                placeholder="文章内容"
              />
              <div
                className="show-html"
                dangerouslySetInnerHTML={{ __html: markdownContent }}
              ></div>
            </Col>
          </Row>
        </Col>

        <Col span={6}>
          <Row>
            <Col span={24}>
              <Button size="large">暂存文章</Button>&nbsp;
              <Button type="primary" size="large" onClick={saveArticle}>
                发布文章
              </Button>
              <br />
            </Col>
            <Col span={24}>
              <br />
              <TextArea
                rows={4}
                value={introducemd}
                onChange={changeIntroduce}
                onPressEnter={changeIntroduce}
                placeholder="文章简介"
              />
              <div
                className="introduce-html"
                dangerouslySetInnerHTML={{
                  __html: "文章简介：" + introducehtml,
                }}
              ></div>
            </Col>

            <Col span={12}>
              <div className="date-select">
                <DatePicker onChange={(data, dataString) => setShowDate(dataString)} placeholder="发布日期" size="large" />
              </div>
            </Col>
          </Row>
        </Col>
      </Row>
    </div>
  );
};

export default AddArticle;
